import java.util.Arrays;

public class Solution {
    public int[] insertionSort2(int[] list, int n) {
        int count = 0;
        for (int i = 1; i < list.length; i++) {

            int sortLen = i;
            int cur = list[i];
            int j = sortLen - 1;
            for (; j >= 0; j--) {
                if (list[j] > cur) {
                    list[j + 1] = list[j];
                } else {
                    break;
                }
            }
            if (j + 1 == i) {
                continue;
            }
            list[j + 1] = cur;
            count++;
            // check loop times
            System.out.println("   " + Arrays.toString(list));
            if (count >= n) {
                break;
            }

        }
        return list;
    }

    public int[] insertionSort(int[] list, int n) {
        int count = 0;
        for (int i = 1; i < list.length; i++) {
            int cur = i;
            for (int j = i - 1; j >= 0; j--) {
                if (list[j] > list[cur]) {
                    int tmp = list[cur];
                    list[cur] = list[j];
                    list[j] = tmp;
                    cur = j;
                    count++;
                    System.err.println("   " + Arrays.toString(list));
                    if (count >= n) {
                        return list;
                    }
                } else {
                    break;
                }
            }
            // check loop times


        }
        return list;
    }

    public static void main(String[] args) {
        //11,12,13,5,6
        System.out.println(Arrays.toString(new Solution().insertionSort(new int[]{12, 11, 13, 5, 6}, 1)));
        //5,11,6,12,13
        System.out.println(Arrays.toString(new Solution().insertionSort(new int[]{12, 11, 13, 5, 6}, 6)));
        System.out.println(Arrays.toString(new Solution().insertionSort(new int[]{250, 343, 137, 789, 551, 954}, 2)));
    }
}
